// Copyright 2009 Google Inc. All Rights Reserved.
package com.google.wave.extensions.polly;

import java.util.logging.Logger;

import com.google.wave.api.Blip;
import com.google.wave.api.ElementType;
import com.google.wave.api.FormElement;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.StyleType;
import com.google.wave.api.StyledText;
import com.google.wave.api.Wavelet;

/**
 * The Poll wavelet is distributed to each poll recipient and allows them to
 * vote/respond to the poll question.
 * 
 * @author scovitz@google.com (Seth Covitz)
 */
public class CaseWavelet {

  /**
   * A reference to the wavelet that received the event or represents the
   * poll.
   */
  private Wavelet wavelet;
  
  /**
   * The persistent state of the poll. 
   */
  public CaseMetadata metadata;
 
  /**
   * A convenient wrapper to query/manipulate a virtual pane in the
   * poll. The PreviewPane is reused from the AdminWavelet such that the
   * poll is identical to what the user saw in the preview.
   */
  private PreviewPane casePane;

	private static final Logger LOG = Logger.getLogger(CaseyServlet.class.getName());
  
  
  
  /**
   * Constructs a PollWavelet in the current robot context. This constructor
   * is called when the PollWavelet receives an event.
   * 
   * @param context the context within which the robot is currently running.
   */
  public CaseWavelet(RobotMessageBundle context) {
    this.wavelet = context.getWavelet();
    this.metadata = new CaseMetadata(context);
    
    Blip rootBlip = wavelet.getRootBlip();
    this.casePane = new PreviewPane(rootBlip, metadata);
  }

  /**
   * Constructs a PollWavelet given the current robot context and a newly
   * created Wavelet. New PollMetadata is created for each poll.
   * 
   * @param context the context within which the robot is currently running.
   * @param wavelet a new wavelet to be initialized as a poll.
   */
  public CaseWavelet(RobotMessageBundle context, Wavelet wavelet) {
    this.wavelet = wavelet;
    this.metadata = new CaseMetadata(context);
    this.casePane = new PreviewPane(wavelet.getRootBlip(), metadata);
  }

  public String serializeMetaData() {
	  return metadata.serializeNewDocument();
  }
  /**
   * Determines whether the wavelet that received an event has been initialized
   * by Polly as a Poll Wavelet.
   * 
   * @param context the context within which the robot is currently running.
   * @return true if the wavelet is a poll wavelet, false otherwise.
   */
  public static boolean isCaseWavelet(RobotMessageBundle context) {
    return context.getWavelet().getRootBlip().getDocument().hasAnnotation("case-wavelet");
  }

  /**
   * Creates the poll form. The poll form consists of a single poll pane that
   * allows the recipient to vote on the poll and submit their vote when done.
   */
  public void create(Wavelet resultsWavelet, String recipient) {
    metadata.setResultsWaveletId(resultsWavelet.getWaveId() + " " +
        resultsWavelet.getWaveletId());

    wavelet.setTitle(new StyledText("Convert to Case", StyleType.HEADING2));
    casePane.create(false);
    wavelet.getRootBlip().getDocument().setAnnotation("case-wavelet", "");
    
    metadata.saveMetadata(wavelet);
  }

  public void create() {
	  	LOG.warning("Wave Id: " + wavelet.getWaveId() + "\nWavelet Id: " + wavelet.getWaveletId() + "\n\n");
	    metadata.setResultsWaveletId(wavelet.getWaveId() + " " + wavelet.getWaveletId());
	    wavelet.setTitle(new StyledText("Convert to Case", StyleType.HEADING2));
	    casePane.create(false);
	    wavelet.getRootBlip().getDocument().setAnnotation("case-wavelet", "");
	    metadata.setBlipId(casePane.getBlipId());
	    metadata.saveMetadata(wavelet);
	  
  }
  /**
   * A handler for PollWavelet events.
   */
  public void handleEvents() {
	  System.out.println("Handling some kind of event...");
	  if (wavelet.getRootBlip().getDocument().hasAnnotation("case-number")) {
		  // do nothing
	  }
	  if (!wavelet.getRootBlip().getDocument().hasAnnotation("case-number")) {
		  wavelet.getRootBlip().getDocument().setAnnotation("case-number", "cs-001");
		  wavelet.appendBlip("make a case out of this???");
		  
	  }
  }
  
  public void handleCaseCreation() {
    if (casePane.isSubmitPollButtonPressed()) {
        submitCase();
      }
  }

  /**
   * Submits the user's vote by writing the vote to the results wavelet's (aka
   * Admin Wavelet) data document.
   */
  private void submitCase() {
	  // This is where we want to create the case in sfdc and save the url and number
	  String caseNumber = "558i4532234";
	  String caseUrl = "http://www.advancedhr.com";
	  LOG.warning("Submitting case...");
	  metadata.writeCaseDataToResults(caseNumber, caseUrl);
	  LOG.warning("Saved the new data to the blip...");
	  wavelet.getRootBlip().getDocument().setAnnotation("case-number", caseNumber);
	  LOG.warning("Setting the annotation to indicate that this thing has a case number");
	  //wavelet.get
	  wavelet.getRootBlip().getDocument().replace("");
/*	  FormElement button = wavelet.getRootBlip().getDocument().getFormView().getFormElement("submit_poll_button");
	  button.setType(ElementType.LABEL);
	  button.setLabel(caseNumber);*/
	  LOG.warning("Leaving the submit case function.");
	  
  }
}
